package com.lemon.smartsafety.ui.fragment.safe.safe_manager;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.event.safe_manager.SafeItemRefreshEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.entity.safe.safe_manager.CompanySafeManageItemVo;
import com.lemon.smartsafety.http.service.SafeManagerItemServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.activity.safe.data.safe_manager.SafeManagerPropActivity;
import com.lemon.smartsafety.ui.adapter.safe.safe_manager.SafeManagerItemAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/2/21.`
 * 安全管理设置一览
 */
public class SafeManagerListFragment extends BasePullToRefreshFragment<CompanySafeManageItemVo, List<CompanySafeManageItemVo>> implements RecyclerTClickListener<CompanySafeManageItemVo> {

    private LoggerHelper loggerHelper = new LoggerHelper(SafeManagerListFragment.class);
    private CompanyInfo mCompanyInfo;

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        if (getArguments() != null) {
            mCompanyInfo = (CompanyInfo) getArguments().getSerializable(BundleKey.COMPANY_INFO);
        }
    }

    public static Fragment newInstance() {
        SafeManagerListFragment fragment = new SafeManagerListFragment();
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_refresh_no_title_no_divider;
    }

    @Override
    protected void initViews() {
        pageSize = 1000;
        super.initViews();
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<CompanySafeManageItemVo> getLemonAdapter() {
        return new SafeManagerItemAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return EventTag.TAG_SAFE_MANAGER;
    }

    @Override
    public void onPullRefresh() {
        SafeManagerItemServices.init().getItemList(RefreshType.REFRESH, pageSize, currentPage);
    }


    @Override
    public void onLoadMore() {
        SafeManagerItemServices.init().getItemList(RefreshType.LOAD_MORE, pageSize, currentPage);
    }

    /**
     * 刷新事件
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(SafeItemRefreshEvent event) {
        if (SafeItemRefreshEvent.class.getName().equals(event.getTag())) {
            mPullToRefreshRecyclerView.beginRefreshing();
        }
    }

    @Override
    public void onItemClick(CompanySafeManageItemVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
        bundle.putSerializable(BundleKey.SAFE_MANAGER_ITEM, data);
        UIHelper.showActivity(getActivity(), bundle, SafeManagerPropActivity.class);
    }
}
